package com.health.patient.server.service.impl;

import com.health.common.core.web.domain.AjaxResult;
import com.health.health.common.dto.PmsMessTextingDto;
import com.health.patient.server.mapper.MessageMapper;
import com.health.patient.server.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Transactional
    @Override
    public AjaxResult sendMessage(PmsMessTextingDto messTextingDto) throws Exception {

        //获取医生ID
        //Long doctorId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        Long doctorId = 1L;

        //添加PmsMessTexting表
        messTextingDto.setDoctorId(doctorId);
        int messTextId = messageMapper.insertMessage(messTextingDto);
        String patientRecordIds = messTextingDto.getPatientRecordIds();
        String[] patientRecordIdArray = patientRecordIds.split(",");
        //添加PmsPatientAndMessage中间表
        int j = messageMapper.insertPatientAndMessage(doctorId,patientRecordIdArray,messTextId);
        if (messTextId!=0 && j==patientRecordIdArray.length){
            return AjaxResult.success();
        }else {
            throw new Exception("发送失败");
        }

    }
}
